/**
 *	流程模块主Flow操作记录
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Flow;




import com.framerock.db.DB;
import com.framerock.baselib.CDate;
import com.framerock.baselib.Jiami;
import com.framerock.baselib.Rock;
import com.framework.Chajian.ChajianReim;
import com.framework.ACore.Model;
import com.framework.Model.ModelDept;
import com.framework.Model.ModelReim;

import java.util.ArrayList;
import java.util.Map;


public class FlowTodo extends FlowBase {

    /**
     * 提交时
     * */
    public static String
            ACTTYPE_TURN = "turn",
    /**
     * 编辑时
     * */
            ACTTYPE_EDIT = "edit",
    /**
     * 删除时
     * */
            ACTTYPE_DEL  = "delete",
    /**
     * 作废时
     * */
            ACTTYPE_ZUOFEI  = "zuofei",
    /**
     * 审批完成时
     * */
            ACTTYPE_FINISH  = "finish",
    /**
     * 计划任务触发时
     * */
            ACTTYPE_TASK    = "task",
    /**
     * 下一步处理人员时
     * */
            ACTTYPE_NEXT    = "next",
    /**
     * 不同意时
     * */
        ACTTYPE_DISSENT    = "dissent",
    /**
     * 评论时
     * */
            ACTTYPE_PING   = "ping";


    private Model tfModel,tModel;

    public FlowTodo(Flow flows) {
        super(flows);
        this.tfModel    = Model.get(this.nDb, "flowtodo");
        this.tModel     = Model.get(this.nDb, "todo");
    }


    private ArrayList<Map<String, String>> rowsTodo = null;

    /**
     * 触发单据通知
     * @param act 触发类型
     * */
    public void sendTodo(String act)
    {
        if(rowsTodo == null)rowsTodo = this.tfModel.getAll("`modeid`="+mFlow.modeid+" AND `status`=1 AND `cid`="+this.COMPANYID+" AND `cid`>0");
        if(rowsTodo == null)return;
        Map<String, String> rs;
        for(int i=0; i<rowsTodo.size();i++){
            rs = rowsTodo.get(i);
            if(Rock.contain(rs.get("acttype"), act)){
                this.runTodo(rs);
            }
        }
    }

    /**
     * 运行发送
     * */
    public void runTodo(Map<String, String> rs)
    {
        String wlx = rs.get("wherelx");
        if(!Rock.isEmpt(wlx)){
            String where = mFlow.getFlowwhere(wlx);
            if(!Rock.isEmpt(where)){
                int to = mFlow.mModel.getCount("`id`="+mFlow.mid+" AND "+where+"");
                if(to == 0)return;
            }
        }
        String receid   = rs.get("receid"),str;
        String tofields = rs.get("tofields");
        if(Rock.getMapString(rs, "toaid").equals("1")){
            str = Rock.getMapString(mFlow.data, "aid", "0");
            if(Integer.parseInt(str) > 0){
                if(!Rock.isEmpt(receid))receid += ",";
                receid += str;
            }
        }
        if(!Rock.isEmpt(tofields)){
            String arr[] = tofields.split(",");
            for(int i=0;i<arr.length;i++){
                str = Rock.getMapString(mFlow.data, arr[i]);
                if(!Rock.isEmpt(str)){
                    if(!Rock.isEmpt(receid))receid += ",";
                    receid += str;
                }
            }
        }
        if(Rock.isEmpt(receid))return;
        String title = Rock.getMapString(rs, "name");
        if(Rock.isEmpt(title))title = mFlow.modename;
        title        = Rock.replaceString(title, mFlow.data);

        String mess  = Rock.getMapString(rs, "summary");
        mess         = Rock.replaceString(mess, mFlow.data);
        this.addTodo(receid, title, mess);
    }

    /**
     * 发送流程通知
     * @param type 类型，todo提醒给下一步
     * @param receid 接收人id，单位人员id
     * @param mess 消息内容
     * */
    public void flowTodo(String type, String receid, String title, String mess)
    {
        if(Rock.isEmpt(title))title = mFlow.modename;
        if(Rock.isEmpt(mess)) {
            if (type.equals(ACTTYPE_TURN) || type.equals(ACTTYPE_NEXT)) {
                mess = "你有["+mFlow.billrs.get("aname")+"]的["+mFlow.modename+"]单据需要去处理。";
            }
            if(type.equals(ACTTYPE_DISSENT)){
                mess = "你提交的["+mFlow.modename+"]单据，["+mRequest.USEANAME+"]处理不同意。";
            }
            if(type.equals(ACTTYPE_FINISH)){
                mess = "你提交的["+mFlow.modename+"]单据，已全部处理完成。";
            }
        }
        addTodo(receid, title, mess);
    }

    /**
     * 给对应人发通知
     * @param receid 接收人id，单位人员id
     * @param title 通知标题
     * @param mess 通知内容
     * */
    public void addTodo(String receid, String title, String mess)
    {
        if(Rock.isEmpt(receid))return;
        String where = mFlow.modelUsera.dept().receidWhere(receid);
        if(Rock.isEmpt(where))return;
        Map<String, String> as,ida,da = Rock.getMap(new String[]{
                "title", title,
                "mess", mess,
                "optdt", CDate.now(),
                "mknum", mFlow.modenum,
                "mid", mFlow.mid,
                "funitid",mFlow.funitid,
                "status", "0",
                "cid", this.COMPANYID
        });

        where = "`cid`="+this.COMPANYID+" AND `status`=1 AND "+where+"";
        String dwhere = DB.dbSql(DB.chuliPerfix("usera"), "id", where);
        this.tModel.delete("`cid`="+this.COMPANYID+" AND `mknum`='"+mFlow.modenum+"' AND `mid`="+mFlow.mid+" AND `status`=0 AND `aid` in("+dwhere+")");

        ArrayList<Map<String, String>> rows = mFlow.modelUsera.getAll(where);
        String uids = "";
        int uid,i;
        ModelReim reimobj = new ModelReim(this.nDb);
        String idaa[]= new String[]{
                "type", "4",
                "optdt",CDate.now(),
                "lastcont", Jiami.base64encode(mess),
                "title", Jiami.base64encode(title),
                "cid", this.COMPANYID
        };
        if(rows!=null)for(i=0;i<rows.size();i++){
            as = rows.get(i);
            da.put("aid", as.get("id"));
            this.tModel.insertbo(da);
            uid= Rock.getMapint(as, "uid");
            if(uid > 0) {
                if (!Rock.isEmpt(uids)) uids += ",";
                uids += "p"+uid+",m"+uid+",r"+uid+""; //p=电脑端,m手机端,r客户端,aAPP的
            }

            //写入到imhistory
            if(mFlow.unitrs != null) {
                ida = Rock.getMap(idaa);
                ida.put("receid", as.get("id"));
                ida.put("sendid",mFlow.funitid);
                reimobj.addHistory(ida);
            }
        }
        //服务端推送
        if(!Rock.isEmpt(uids)){
            String mkname   = Rock.getMapString(mFlow.unitrs, "name", mFlow.modename);
            String icon     = Rock.getMapString(mFlow.unitrs, "icons", mFlow.moders.get("imgurl"));
            ChajianReim.obj(this.nDb).push(Rock.getMap(new String[]{
                    "type","todo",
                    "receid", uids,
                    "title", title,
                    "mess", mess,
                    "sendid", mRequest.USERID,
                    "cnum", mRequest.getCompanyrs("num"),
                    "mknum",mFlow.modenum,
                    "mkname",mkname,
                    "icon",icon,
                    "mid", mFlow.mid,
                    "funitid",mFlow.funitid
            }));
        }
    }
}
